package com.icoolkj.ms.common.core.response;

import cn.hutool.core.util.ObjectUtil;
import com.icoolkj.ms.common.core.enums.ResponseEnum;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author icoolkj
 * @version 1.0
 * @description 响应的统一数据
 * @createDate 2025/02/02 16:26
 */
@Data
public class ServerResponseEntity<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Logger logger = LoggerFactory.getLogger(ServerResponseEntity.class);

    /**
     * 状态码
     */
    private int code;

    /**
     * 信息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    public static <T> ServerResponseEntity<T> success(){
        return restServerResponseEntity(null, ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage());
    }

    public static <T> ServerResponseEntity<T> success(T data) {
        return restServerResponseEntity(data, ResponseEnum.SUCCESS.getCode(), null);
    }

    public static <T> ServerResponseEntity<T> success(T data, String message) {
        return restServerResponseEntity(data, ResponseEnum.SUCCESS.getCode(), message);
    }

    public static <T> ServerResponseEntity<T> fail(String message) {
        return restServerResponseEntity(null, ResponseEnum.INTERNAL_SERVER_ERROR.getCode(), message);
    }

    public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum) {
        ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
        serverResponseEntity.setMessage(responseEnum.getMessage());
        serverResponseEntity.setCode(responseEnum.getCode());
        return serverResponseEntity;
    }

    public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum, T data) {
        ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
        serverResponseEntity.setMessage(responseEnum.getMessage());
        serverResponseEntity.setCode(responseEnum.getCode());
        serverResponseEntity.setData(data);
        return serverResponseEntity;
    }

    public boolean isSuccess() {
        return ObjectUtil.equals(ResponseEnum.SUCCESS.getCode(), this.code);
    }

    public static <T> ServerResponseEntity<T> restServerResponseEntity(T data, int code, String message) {
        ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<T>();
        serverResponseEntity.setData(data);
        serverResponseEntity.setCode(code);
        serverResponseEntity.setMessage(message);
        return serverResponseEntity;
    }

    public static <T> ServerResponseEntity<T> transform(ServerResponseEntity<?> oldServerResponseEntity) {
        return restServerResponseEntity(null, oldServerResponseEntity.getCode(), oldServerResponseEntity.getMessage());
    }
}
